/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.inspur.edp.caf.cef.schema.base.element.serializer.impl.DefaultOperationSerializer;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.caf.cef.schema.element.Operation;
import com.inspur.edp.component.schema.RefStructureInfo;
import com.inspur.edp.lcm.metadata.api.IMetadataContent;
import com.inspur.edp.lcm.metadata.spi.MetadataContentSerializer;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.WSComponent;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.sourceinfo.impl.CafIApiSourceInfo;
import com.inspur.edp.webservicecomponent.metadata.serializer.obsolete.sourceinfo.impl.SgIApiSourceInfo;

import java.util.ArrayList;

public class WSCmpSerializer implements MetadataContentSerializer {

    @Override
    public JsonNode Serialize(IMetadataContent metadataContent) {
//        WebServiceComponent cmp = (WebServiceComponent) metadataContent;
//        JsonNode jobj = new ObjectMapper().createObjectNode();
//        JsonUtil.setPropValue(jobj, "id", cmp.getId());
//        JsonUtil.setPropValue(jobj, "code", cmp.getCode());
//        JsonUtil.setPropValue(jobj, "name", cmp.getName());
//        JsonUtil.setPropValue(jobj, "description", cmp.getDescription());
//        JsonUtil.setPropValue(jobj, "application", cmp.getApplication());
//        JsonUtil.setPropValue(jobj, "serviceUnit", cmp.getServiceUnit());
//        JsonUtil.setPropValue(jobj, "version", cmp.getVersion());
//
//        if (cmp.getOperations() != null) {
//            ArrayNode operationsJArray = JsonUtil.addEmptyArray(jobj, "operations");
//            for (Operation op : cmp.getOperations()) {
//                DefaultOperationSerializer opSerializer = new DefaultOperationSerializer();
//                operationsJArray.add(opSerializer.serialize(op, WSSerializerContext.instance));
//            }
//        }
//
//        if (cmp.getRefStructures() != null) {
//            ArrayNode refStructuresJArray = JsonUtil.addEmptyArray(jobj, "refStructures");
//            for (RefStructureInfo info : cmp.getRefStructures()) {
//                JsonNode jObject = new ObjectMapper().createObjectNode();
//                JsonUtil.setPropValue(jObject, "id", info.getId());
//                JsonUtil.setPropValue(jObject, "referId", info.getReferId());
//                JsonUtil.setPropValue(jObject, "referType", info.getReferType());
//                JsonUtil.setPropValue(jObject, "referCode", info.getReferCode());
//                JsonUtil.setPropValue(jObject, "structuredTypeId", info.getStructuredTypeId());
//                JsonUtil.setPropValue(jObject, "code", info.getCode());
//                JsonUtil.setPropValue(jObject, "name", info.getName());
//                refStructuresJArray.add(jObject);
//            }
//        }
//
//        if (cmp.getSourceInfo() != null) {
//            if (cmp.getSourceInfo() instanceof CafIApiSourceInfo) {
//                ObjectNode jObject = new ObjectMapper().createObjectNode();
//                JsonUtil.setPropValue(jObject, "type", cmp.getSourceInfo().getSourceType());
//                CafIApiSourceInfo cafApiInfo = (CafIApiSourceInfo) cmp.getSourceInfo();
//                JsonUtil.setPropValue(jObject, "serviceUnit", cafApiInfo.getServiceUnit());
//                JsonUtil.setPropValue(jObject, "apiVersion", cafApiInfo.getApiVersion());
//                JsonUtil.setPropValue(jObject, "implementAssembly", cafApiInfo.getImplementAssembly());
//                JsonUtil.setPropValue(jObject, "interfaceAssembly", cafApiInfo.getInterfaceAssembly());
//                JsonUtil.setPropValue(jObject, "isCommonApi", cafApiInfo.getCommonApi());
//                JsonUtil.setPropValue(jobj, "sourceInfo", jObject);
//
//            } else if (cmp.getSourceInfo() instanceof SgIApiSourceInfo) {
//                ObjectNode jObject = new ObjectMapper().createObjectNode();
//                JsonUtil.setPropValue(jObject, "type", cmp.getSourceInfo().getSourceType());
//                SgIApiSourceInfo metadataApiInfo = (SgIApiSourceInfo) cmp.getSourceInfo();
//
//                JsonUtil.setPropValue(jObject, "apiId", metadataApiInfo.getApiId());
//                JsonUtil.setPropValue(jObject, "apiCode", metadataApiInfo.getApiCode());
//                JsonUtil.setPropValue(jObject, "apiName", metadataApiInfo.getApiName());
//                JsonUtil.setPropValue(jObject, "apiSu", metadataApiInfo.getApiSu());
//                JsonUtil.setPropValue(jObject, "apiUri", metadataApiInfo.getApiUri());
//
//                JsonUtil.setPropValue(jobj, "sourceInfo", jObject);
//            } else {
//                throw new RuntimeException("not support sourceType" + cmp.getSourceInfo().getSourceType());
//            }
//        }
//        return jobj;
        return null;
    }

    @Override
    public IMetadataContent DeSerialize(JsonNode jObj) {
        WSComponent cmp = new WSComponent();
        cmp.setId(JsonUtil.getPropValue(jObj, "id", String.class));
        cmp.setCode(JsonUtil.getPropValue(jObj, "code", String.class));
        cmp.setName(JsonUtil.getPropValue(jObj, "name", String.class));
        cmp.setDescription(JsonUtil.getPropValue(jObj, "description", String.class));
        cmp.setApplication(JsonUtil.getPropValue(jObj, "application", String.class));
        cmp.setServiceUnit(JsonUtil.getPropValue(jObj, "serviceUnit", String.class));
        cmp.setVersion(JsonUtil.getPropValue(jObj, "version", String.class));
        if (JsonUtil.isExistsProp(jObj, "operations")) {
            cmp.setOperations(new ArrayList<Operation>());
            ArrayNode opJArray = JsonUtil.getPropValue(jObj, "operations", ArrayNode.class);
            for (JsonNode opJobj : opJArray) {
                DefaultOperationSerializer opSerializer = new DefaultOperationSerializer();
                cmp.getOperations()
                        .add(opSerializer.deSerialize(opJobj, WSSerializerContext.instance));
            }
        }

        if (JsonUtil.isExistsProp(jObj, "refStructures")) {
            cmp.setRefStructures(new ArrayList<RefStructureInfo>());
            ArrayNode refStructureJArray = JsonUtil.getPropValue(jObj, "refStructures", ArrayNode.class);
            for (JsonNode refStructureJobj : refStructureJArray) {
                RefStructureInfo info = new RefStructureInfo();
                info.setId(JsonUtil.getPropValue(refStructureJobj, "id", String.class));
                info.setReferId(JsonUtil.getPropValue(refStructureJobj, "referId", String.class));
                info.setReferCode(JsonUtil.getPropValue(refStructureJobj, "referCode", String.class));
                info.setReferType(JsonUtil.getPropValue(refStructureJobj, "referType", String.class));
                info.setStructuredTypeId(
                        JsonUtil.getPropValue(refStructureJobj, "structuredTypeId", String.class));
                info.setCode(JsonUtil.getPropValue(refStructureJobj, "code", String.class));
                info.setName(JsonUtil.getPropValue(refStructureJobj, "name", String.class));
                cmp.getRefStructures().add(info);
            }
        }

        if (JsonUtil.isExistsProp(jObj, "sourceInfo")) {
            ObjectNode sourceObject = JsonUtil.getPropValue(jObj, "sourceInfo", ObjectNode.class);
            String sourceType = JsonUtil.getPropValue(sourceObject, "type", String.class);
            if (sourceType.equals(CafIApiSourceInfo.SOURCE_TYPE)) {
                CafIApiSourceInfo cafApiInfo = new CafIApiSourceInfo();
                cafApiInfo.setServiceUnit(JsonUtil.getPropValue(sourceObject, "serviceUnit", String.class));
                cafApiInfo.setApiVersion(JsonUtil.getPropValue(sourceObject, "apiVersion", String.class));
                cafApiInfo.setImplementAssembly(JsonUtil.getPropValue(sourceObject, "implementAssembly",
                        String.class));
                cafApiInfo.setInterfaceAssembly(JsonUtil.getPropValue(sourceObject, "interfaceAssembly",
                        String.class));
                cafApiInfo.setCommonApi(JsonUtil.getPropValue(sourceObject, "isCommonApi", boolean.class));
                cmp.setSourceInfo(cafApiInfo);
            } else if (sourceType.equals(SgIApiSourceInfo.SOURCE_TYPE)) {
                SgIApiSourceInfo apiSourceInfo = new SgIApiSourceInfo();
                apiSourceInfo.setApiId(JsonUtil.getPropValue(sourceObject, "apiId", String.class));
                apiSourceInfo.setApiCode(JsonUtil.getPropValue(sourceObject, "apiCode", String.class));
                apiSourceInfo.setApiName(JsonUtil.getPropValue(sourceObject, "apiName", String.class));
                apiSourceInfo.setApiSu(JsonUtil.getPropValue(sourceObject, "apiSu", String.class));
                apiSourceInfo.setApiUri(JsonUtil.getPropValue(sourceObject, "apiUri", String.class));
                cmp.setSourceInfo(apiSourceInfo);
            } else {
                throw new RuntimeException("not support sourceType " + sourceType);
            }
        }
        return cmp;
    }
}
